#include <unistd.h>

#include <i264e_common.h>
#include <jzm_enc_api.h>
#include <jzm_enc_api_t20.h>
#include <jzm_enc_api_t30.h>
#include <jzm_enc_api_t21.h>
#ifdef DMALLOC
#include <imp/dmalloc.h>
#endif
int i264e_ratecontrol_priv_init(i264e_t *h, void **priv, int *priv_size)
{
    *priv = NULL;
    *priv_size = 0;

    if ((h->common.param.soc == C_T21) || (h->common.param.soc == C_T23)){
        unsigned int reg_offset[] = {0x80120, 0x80124,0x80128,0x8012c, 0x80130, 0x80134, 0x80138, 0x8013c, 0x80140, 0x80144, 0x80148, 0x8014c, 0x500e8, 0x500ec, 0x500f0, 0x80080, 0x40094, 0x40098, 0x4009c, 0x400a0,0x800e8, 0x800ec, 0x800e4, /* [22], {ps, pns} */
                                     0x800e0, /* [23], {i4, i16} */
                                     0x80168, /* [24], {pt8, i8} */
                                     0x80114, /* [25] */
                                     0x80118, /* [26] */
                                     0x8011c, /* [27] */
                                     0x80034, /* [28] */
                                     0x800d8, /* [29] */
                                     0x800dc, /* [30] */
                                     0x80084, /* [31] */
                                     0x80088, /* [32] */
                                     0x8008c, /* [33] */
                                     0x80090, /* [34] */
                                     0x80094, /* [35] */
                                     0x800f0, /* [36] */
                                     0x800f4, /* [37] */
                                     0x800f8, /* [38] */
        };//bak step: add addr only
		int i = 0, reg_info_cnt = sizeof(reg_offset) / sizeof(reg_offset[0]);
		unsigned int *reg_info = NULL;

		reg_info = calloc(reg_info_cnt, 2 * sizeof(unsigned int));
		if (reg_info == NULL) {
			i264e_log(h, C_LOG_ERROR, "calloc reg_info cnt=%d failed\n", __func__, reg_info_cnt);
			goto err_calloc_reg_info;
		}
        for (i = 0; i < reg_info_cnt; i++) {
			reg_info[2 * i] = reg_offset[i];
			reg_info[2 * i + 1] = 0;
		}
		*priv = reg_info;
		*priv_size = reg_info_cnt * 2 * sizeof(unsigned int);
	}
    return 0;

    err_calloc_reg_info:
    return -1;
}

void i264e_ratecontrol_priv_deinit(i264e_t *h, void *priv, int priv_size)
{
    if (priv) {
        free(priv);
    }
}

int i264e_analyze_rcfile_param(FILE *fp, char *linebuf, char *tmpbuf1, char *tmpbuf2, RC_CFG_S_T21 *cfg)
{
	char *str = NULL;
	while (NULL != (str = fgets (linebuf, 128, fp))) {
		if (strlen(str) > 128) {
			return -1;
		} else if (2 != sscanf(linebuf, "%[^:]:%s", tmpbuf1, tmpbuf2)) {
			continue;
		}
		char *ch = strchr(tmpbuf1, ' ');
		if (ch) *ch = 0;
		if (0 == strcmp(tmpbuf1, "u16FrmWidth")) {
			cfg->u16FrmWidth = strtol(tmpbuf2, NULL, 0);
			cfg->u16MbWidth  = ((cfg->u16FrmWidth + 15) & ~15) / 16;
		} else if (0 == strcmp(tmpbuf1,"u16FrmHeight")) {
			cfg->u16FrmHeight = strtol(tmpbuf2, NULL, 0);
			cfg->u16MbHeight  = ((cfg->u16FrmHeight + 15) & ~15) / 16;
		} else if (0 == strcmp(tmpbuf1,"rcMode")) {
			cfg->rcMode = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"encProtocol")) {
			cfg->encProtocol = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"gopMode")) {
			cfg->gopMode = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"fskMode")) {
			cfg->fskMode = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"priority")) {
			cfg->priority = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"qpgMode")) {
			cfg->qpgMode = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"intelMode")) {
			cfg->intelMode = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sceMode")) {
			cfg->sceMode = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"s8StartQp")) {
			cfg->s8StartQp = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"superFrmCfg.superFrmMode")) {
			cfg->superFrmCfg.superFrmMode = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"superFrmCfg.u32SuperIFrmBitsThr")) {
			cfg->superFrmCfg.u32SuperIFrmBitsThr = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"superFrmCfg.u32SuperPFrmBitsThr")) {
			cfg->superFrmCfg.u32SuperPFrmBitsThr = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"superFrmCfg.u32SuperBFrmBitsThr")) {
			cfg->superFrmCfg.u32SuperBFrmBitsThr = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"lostFrmCfg.bFrmLostEn")) {
			cfg->lostFrmCfg.bFrmLostEn = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"lostFrmCfg.frmLostMode")) {
			cfg->lostFrmCfg.frmLostMode = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"lostFrmCfg.u32FrmLostBpsThr")) {
			cfg->lostFrmCfg.u32FrmLostBpsThr = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"lostFrmCfg.u32FrmLostGaps")) {
			cfg->lostFrmCfg.u32FrmLostGaps = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"gopNormalP.s8IPQpDelta")) {
			cfg->gopNormalP.s8IPQpDelta = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"gopSmartP.u32BgInterval")) {
			cfg->gopSmartP.u32BgInterval = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"gopSmartP.s8BgQpDelta")) {
			cfg->gopSmartP.s8BgQpDelta = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"gopSmartP.s8ViQpDelta")) {
			cfg->gopSmartP.s8ViQpDelta = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"gopDoulP.u32SPInterval")) {
			cfg->gopDoulP.u32SPInterval = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"gopDoulP.s8SPQpDelta")) {
			cfg->gopDoulP.s8SPQpDelta = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"gopDoulP.s8IPQpDelta")) {
			cfg->gopDoulP.s8IPQpDelta = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"gopBipredP.u32BFrmNum")) {
			cfg->gopBipredP.u32BFrmNum = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"gopBipredP.s8BQpDelta")) {
			cfg->gopBipredP.s8BQpDelta = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"gopBipredP.s8IPQpDelta")) {
			cfg->gopBipredP.s8IPQpDelta = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrCqp.u32Gop")) {
			cfg->attrH264Cqp.u32Gop = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrCqp.u32SrcFrmRate")) {
			cfg->attrH264Cqp.u32SrcFrmRate = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrCqp.frDstFrmRate")) {
			cfg->attrH264Cqp.frDstFrmRate = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrCqp.u8IQp")) {
			cfg->attrH264Cqp.u8IQp = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrCqp.u8PQp")) {
			cfg->attrH264Cqp.u8PQp = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrCbr.u32Gop")) {
			cfg->attrH264Cbr.u32Gop = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrCbr.u32StatTime")) {
			cfg->attrH264Cbr.u32StatTime = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrCbr.u32SrcFrmRate")) {
			cfg->attrH264Cbr.u32SrcFrmRate = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrCbr.frDstFrmRate")) {
			cfg->attrH264Cbr.frDstFrmRate = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrCbr.u32BitRate")) {
			cfg->attrH264Cbr.u32BitRate = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrCbr.u8FluctuateLevel")) {
			cfg->attrH264Cbr.u8FluctuateLevel = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramCbr.u8MaxIprop")) {
			cfg->paramH264Cbr.u8MaxIprop = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramCbr.u8MinIprop")) {
			cfg->paramH264Cbr.u8MinIprop = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramCbr.u8MaxReEncodeTimes")) {
			cfg->paramH264Cbr.u8MaxReEncodeTimes = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramCbr.u8MaxQp")) {
			cfg->paramH264Cbr.u8MaxQp = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramCbr.u8MinQp")) {
			cfg->paramH264Cbr.u8MinQp = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramCbr.u8MaxIQp")) {
			cfg->paramH264Cbr.u8MaxIQp = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramCbr.u8MinIQp")) {
			cfg->paramH264Cbr.u8MinIQp = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramCbr.s8IPQpDelta")) {
			cfg->paramH264Cbr.s8IPQpDelta = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramCbr.u8QualityLvl")) {
			cfg->paramH264Cbr.u8QualityLvl = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramCbr.u8MaxIPQpDelta")) {
			cfg->paramH264Cbr.u8MaxIPQpDelta = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramCbr.u8MaxPPQpDelta")) {
			cfg->paramH264Cbr.u8MaxPPQpDelta = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramCbr.s8IQpBias")) {
			cfg->paramH264Cbr.s8IQpBias = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrVbr.u32Gop")) {
			cfg->attrH264Vbr.u32Gop = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrVbr.u32StatTime")) {
			cfg->attrH264Vbr.u32StatTime = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrVbr.u32SrcFrmRate")) {
			cfg->attrH264Vbr.u32SrcFrmRate = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrVbr.frDstFrmRate")) {
			cfg->attrH264Vbr.frDstFrmRate = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrVbr.u32MaxBitRate")) {
			cfg->attrH264Vbr.u32MaxBitRate = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrVbr.u8MaxQp")) {
			cfg->attrH264Vbr.u8MaxQp = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrVbr.u8MinQp")) {
			cfg->attrH264Vbr.u8MinQp = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrVbr.u8MinIQp")) {
			cfg->attrH264Vbr.u8MinIQp = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"attrVbr.s8AvbrEn")) {
			cfg->attrH264Vbr.s8AvbrEn = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramVbr.u8ChangePos")) {
			cfg->paramH264Vbr.u8ChangePos = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramVbr.u8MaxIprop")) {
			cfg->paramH264Vbr.u8MaxIprop = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramVbr.u8MinIprop")) {
			cfg->paramH264Vbr.u8MinIprop = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramVbr.u8MaxReEncodeTimes")) {
			cfg->paramH264Vbr.u8MaxReEncodeTimes = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramVbr.s8IPQpDelta")) {
			cfg->paramH264Vbr.s8IPQpDelta = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramVbr.u8MaxIPQpDelta")) {
			cfg->paramH264Vbr.u8MaxIPQpDelta = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramVbr.u8MaxPPQpDelta")) {
			cfg->paramH264Vbr.u8MaxPPQpDelta = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramVbr.u8QualityLvl")) {
			cfg->paramH264Vbr.u8QualityLvl = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramVbr.s8RQDelta")) {
			cfg->paramH264Vbr.s8RQDelta = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"paramVbr.s8IQpBias")) {
			cfg->paramH264Vbr.s8IQpBias = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.IQpMode")) {
			cfg->rcInside.IQpMode = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.fstPQpMode")) {
			cfg->rcInside.fstPQpMode = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.avgPQpMode")) {
			cfg->rcInside.avgPQpMode = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.qpModel")) {
			cfg->rcInside.qpModel = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.bitrLvlEn")) {
			cfg->rcInside.bitrLvlEn = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.sceJudgeLvl")) {
			cfg->rcInside.sceJudgeLvl = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.sceJudgeEn")) {
			cfg->rcInside.sceJudgeEn = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.useFstPEn")) {
			cfg->rcInside.useFstPEn = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.avgQpNewEn")) {
			cfg->rcInside.avgQpNewEn = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.avgPQpNum")) {
			cfg->rcInside.avgPQpNum = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.qpFluctEn")) {
			cfg->rcInside.qpFluctEn = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.qpFluctLvl")) {
			cfg->rcInside.qpFluctLvl = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.fluctLvl")) {
			cfg->rcInside.fluctLvl = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.bndEn")) {
			cfg->rcInside.bndEn = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.BndLvl")) {
			cfg->rcInside.BndLvl = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.stmLvl")) {
			cfg->rcInside.stmLvl = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.t0Method")) {
			cfg->rcInside.t0Method = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.avgPBitsNum")) {
			cfg->rcInside.avgPBitsNum = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.gammaEn")) {
			cfg->rcInside.gammaEn = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.stillSize")) {
			cfg->rcInside.stillSize = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.moveSize")) {
			cfg->rcInside.moveSize = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.qpgIMode")) {
			cfg->rcInside.qpgIMode = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"rcInside.qpgPMode")) {
			cfg->rcInside.qpgPMode = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.frm_re0A")) {
			cfg->siParamH264.frm_re0A = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.frm_re1A")) {
			cfg->siParamH264.frm_re1A = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.frm_re2A")) {
			cfg->siParamH264.frm_re2A = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.frm_re3A")) {
			cfg->siParamH264.frm_re3A = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.scl")) {
			cfg->siParamH264.scl = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.hpel_en")) {
			cfg->siParamH264.hpel_en = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.qpel_en")) {
			cfg->siParamH264.qpel_en = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.max_sech_step_i")) {
			cfg->siParamH264.max_sech_step_i = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.max_mvrx_i")) {
			cfg->siParamH264.max_mvrx_i = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.max_mvry_i")) {
			cfg->siParamH264.max_mvry_i = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.fs_en")) {
			cfg->siParamH264.fs_en = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.fs_md")) {
			cfg->siParamH264.fs_md = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.fs_px")) {
			cfg->siParamH264.fs_px = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.fs_py")) {
			cfg->siParamH264.fs_py = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.fs_rx")) {
			cfg->siParamH264.fs_rx = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.fs_ry")) {
			cfg->siParamH264.fs_ry = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.frm_mv_en")) {
			cfg->siParamH264.frm_mv_en = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.frm_mv_size")) {
			cfg->siParamH264.frm_mv_size = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.glb_mv_en")) {
			cfg->siParamH264.glb_mv_en = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.me_step_en")) {
			cfg->siParamH264.me_step_en = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.me_step_0")) {
			cfg->siParamH264.me_step_0 = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.me_step_1")) {
			cfg->siParamH264.me_step_1 = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.deadzone0Aiy4")) {
			cfg->siParamH264.deadzone0Aiy4 = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.deadzone1Apy4")) {
			cfg->siParamH264.deadzone1Apy4 = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.deadzone2Aic4")) {
			cfg->siParamH264.deadzone2Aic4 = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.deadzone3Apc4")) {
			cfg->siParamH264.deadzone3Apc4 = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.deadzone4Aiy8")) {
			cfg->siParamH264.deadzone4Aiy8 = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.deadzone5Apy8")) {
			cfg->siParamH264.deadzone5Apy8 = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.deadzone6Aydc")) {
			cfg->siParamH264.deadzone6Aydc = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.deadzone7Aicdc")) {
			cfg->siParamH264.deadzone7Aicdc = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.deadzone8Apcdc")) {
			cfg->siParamH264.deadzone8Apcdc = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.acmask_mode")) {
			cfg->siParamH264.acmask_mode = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.deblock")) {
			cfg->siParamH264.deblock = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.alpha_c0_offset")) {
			cfg->siParamH264.alpha_c0_offset = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.beta_offset")) {
			cfg->siParamH264.beta_offset = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.jm_lambda2_en")) {
			cfg->siParamH264.jm_lambda2_en = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.inter_nei_en")) {
			cfg->siParamH264.inter_nei_en = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.skip_bias_en")) {
			cfg->siParamH264.skip_bias_en = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.dcm_en")) {
			cfg->siParamH264.dcm_en = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.dcm_param")) {
			cfg->siParamH264.dcm_param = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.use_intra_in_pframe")) {
			cfg->siParamH264.use_intra_in_pframe = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.cqp_offset")) {
			cfg->siParamH264.cqp_offset = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.mb_mode_useMASK")) {
			cfg->siParamH264.mb_mode_useMASK = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.sas_eigen_en")) {
			cfg->siParamH264.sas_eigen_en = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.crp_eigen_en")) {
			cfg->siParamH264.crp_eigen_en = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.refresh_enMASK")) {
			cfg->siParamH264.refresh_enMASK = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.refresh_mode")) {
			cfg->siParamH264.refresh_mode = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.refresh_bias")) {
			cfg->siParamH264.refresh_bias = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.refresh_cplx_thd")) {
			cfg->siParamH264.refresh_cplx_thd = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.force_i16dc")) {
			cfg->siParamH264.force_i16dc = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.i16dc_cplx_thdIDR")) {
			cfg->siParamH264.i16dc_cplx_thdIDR = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.i16dc_cplx_thdELSE")) {
			cfg->siParamH264.i16dc_cplx_thdELSE = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.i16dc_qp_base")) {
			cfg->siParamH264.i16dc_qp_base = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.i16dc_qp_sel")) {
			cfg->siParamH264.i16dc_qp_sel = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.force_i16")) {
			cfg->siParamH264.force_i16 = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.i16_qp_baseIDR")) {
			cfg->siParamH264.i16_qp_baseIDR = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.i16_qp_baseELSE")) {
			cfg->siParamH264.i16_qp_baseELSE = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.i16_qp_sel")) {
			cfg->siParamH264.i16_qp_sel = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.rrs_enMASK")) {
			cfg->siParamH264.rrs_enMASK = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.rrs_uv_enMASK")) {
			cfg->siParamH264.rrs_uv_enMASK = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.rrs_size_y")) {
			cfg->siParamH264.rrs_size_y = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.rrs_size_c")) {
			cfg->siParamH264.rrs_size_c = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.skin_dt_en")) {
			cfg->siParamH264.skin_dt_en = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.skin_lvl")) {
			cfg->siParamH264.skin_lvl = strtol(tmpbuf2, NULL, 0);
		} else if (0 == strcmp(tmpbuf1,"sip.skin_cnt_thd")) {
			cfg->siParamH264.skin_cnt_thd = strtol(tmpbuf2, NULL, 0);
		}
	}
	return 0;
}

int i264e_rcfile_config(i264e_t *h)
{
	FILE *fp = NULL;
	char linebuf[128];
	char tmpbuf1[64];
	char tmpbuf2[64];
	char filepath[15] = "/tmp/rc.config";

	if ((h->common.param.soc == C_T21) || (h->common.param.soc == C_T23)) {
		memset(linebuf, 0, 128);
		memset(tmpbuf1, 0, 64);
		memset(tmpbuf2, 0, 64);

		fp = fopen(filepath, "r");
		if (NULL == fp) {
			i264e_log(h, C_LOG_ERROR, "%s(%d):err:fopen err\n", __func__, __LINE__);
			return -1;
		}
		if (i264e_analyze_rcfile_param(fp, linebuf, tmpbuf1, tmpbuf2, &h->common.eprc_t21.cfg) != 0) {
			fclose(fp);
			return -1;
		}
		fclose(fp);
	}else {
		i264e_log(h, C_LOG_ERROR, "%s(%d):soc is not T21\n", __func__, __LINE__);
		return -1;
	}
	return 0;
}

int i264e_ratecontrol_init(i264e_t *h, int initQp)
{
    /*setup para */
    int is_rcattr_exist = !access("/tmp/rcattr", F_OK);

    if ((h->common.param.soc == C_T21) || (h->common.param.soc == C_T23)) {
        h->common.eprc_t21.cfg.encProtocol                     = ENC_H264;
        h->common.eprc_t21.cfg.gopMode                         = h->common.param.rc.i_rc_method == I264E_RC_SMART ? SMARTP : NORMALP;
        h->common.eprc_t21.cfg.u16FrmWidth                     = h->common.param.i_width;
        h->common.eprc_t21.cfg.u16FrmHeight                    = h->common.param.i_height;
        eprc_default_set_T21(&h->common.eprc_t21);
        if (h->common.param.rc.i_rc_method == I264E_RC_CQP)
            h->common.eprc_t21.cfg.qpgMode = 0x0;
        h->common.eprc_t21.cfg.u16MbWidth                      = ((h->common.param.i_width + 15) & ~15) / 16;
        h->common.eprc_t21.cfg.u16MbHeight                     = ((h->common.param.i_height + 15) & ~15) / 16;
        h->common.eprc_t21.cfg.fskMode                         = h->common.param.rc.i_rc_method == I264E_RC_SMART ? GMODE_H1M_FALSE : GMODE_N1X;
        h->common.eprc_t21.cfg.intelMode                       = INTEL_CLOSE;
        h->common.eprc_t21.cfg.superFrmCfg.superFrmMode        = SUPERFRM_REENCODE;
        h->common.eprc_t21.cfg.superFrmCfg.u32SuperIFrmBitsThr = h->common.param.superFrm.iframe_bits_thresd;// / 1024;
        h->common.eprc_t21.cfg.superFrmCfg.u32SuperPFrmBitsThr = h->common.param.superFrm.pframe_bits_thresd;// / 1024;
        h->common.eprc_t21.cfg.superFrmCfg.u32SuperBFrmBitsThr = h->common.param.superFrm.pframe_bits_thresd;// / 1024;
        h->common.eprc_t21.cfg.lostFrmCfg.bFrmLostEn           = 0;
        h->common.eprc_t21.cfg.lostFrmCfg.frmLostMode          = FRMLOST_PSKIP;
        h->common.eprc_t21.cfg.lostFrmCfg.u32FrmLostBpsThr     = 0xffffffff;
        h->common.eprc_t21.cfg.lostFrmCfg.u32FrmLostGaps       = 2;
        h->common.eprc_t21.cfg.bBufShare                       = h->common.param.b_buf_share;
        switch(h->common.eprc_t21.cfg.gopMode) {
        case NORMALP:
            h->common.eprc_t21.cfg.gopNormalP.s8IPQpDelta  = 3;
            break;
        case SMARTP:
            h->common.eprc_t21.cfg.gopSmartP.u32BgInterval = h->common.param.i_gop * h->common.skip_h.mscenecnt;
            h->common.eprc_t21.cfg.gopSmartP.s8BgQpDelta   = 3;
            h->common.eprc_t21.cfg.gopSmartP.s8ViQpDelta   = 3;
            break;
        case DOULP:
            h->common.eprc_t21.cfg.gopDoulP.u32SPInterval  = 0;
            h->common.eprc_t21.cfg.gopDoulP.s8SPQpDelta    = 3;
            h->common.eprc_t21.cfg.gopDoulP.s8IPQpDelta    = 3;
            break;
        case BIPREDP:
            h->common.eprc_t21.cfg.gopBipredP.u32BFrmNum   = 1;
            h->common.eprc_t21.cfg.gopBipredP.s8BQpDelta   = 3;
            h->common.eprc_t21.cfg.gopBipredP.s8IPQpDelta  = 3;
            break;
        default:
            break;
        }
        if (h->common.param.rc.i_rc_method == I264E_RC_CQP) {
            h->common.eprc_t21.cfg.rcMode                    = T21_CQP;
            h->common.eprc_t21.cfg.attrH264Cqp.u32Gop        = h->common.param.i_gop;
            h->common.eprc_t21.cfg.attrH264Cqp.u32SrcFrmRate = h->common.param.i_fps_num;
            h->common.eprc_t21.cfg.attrH264Cqp.frDstFrmRate  = h->common.param.i_fps_den;
            h->common.eprc_t21.cfg.attrH264Cqp.u8IQp         = h->common.param.rc.i_qp_constant - 3;
            h->common.eprc_t21.cfg.attrH264Cqp.u8PQp         = h->common.param.rc.i_qp_constant;
            if (is_rcattr_exist) {
                i264e_log(h, C_LOG_INFO, "t21.cfg.rcMode=%u\n",                    h->common.eprc_t21.cfg.rcMode);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cqp.u32Gop=%u\n",        h->common.eprc_t21.cfg.attrH264Cqp.u32Gop);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cqp.u32SrcFrmRate=%u\n", h->common.eprc_t21.cfg.attrH264Cqp.u32SrcFrmRate);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cqp.frDstFrmRate=%u\n",  h->common.eprc_t21.cfg.attrH264Cqp.frDstFrmRate);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cqp.u8IQp=%u\n",         h->common.eprc_t21.cfg.attrH264Cqp.u8IQp);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cqp.u8PQp=%u\n",         h->common.eprc_t21.cfg.attrH264Cqp.u8PQp);
            }
        }
        else if (h->common.param.rc.i_rc_method == I264E_RC_CBR) {
            h->common.eprc_t21.cfg.rcMode                          = T21_CBR;
            h->common.eprc_t21.cfg.attrH264Cbr.u32Gop              = h->common.param.i_gop;
            h->common.eprc_t21.cfg.attrH264Cbr.u32StatTime         = h->common.param.rc.i_static_time;
            h->common.eprc_t21.cfg.attrH264Cbr.u32SrcFrmRate       = h->common.param.i_fps_num;
            h->common.eprc_t21.cfg.attrH264Cbr.frDstFrmRate        = h->common.param.i_fps_den;
            h->common.eprc_t21.cfg.attrH264Cbr.u32BitRate          = h->common.param.rc.i_bitrate;
            h->common.eprc_t21.cfg.attrH264Cbr.u8FluctuateLevel    = 3;
            h->common.eprc_t21.cfg.paramH264Cbr.u8MaxIprop         = 100;
            h->common.eprc_t21.cfg.paramH264Cbr.u8MinIprop         = 1;
            h->common.eprc_t21.cfg.paramH264Cbr.u8MaxReEncodeTimes = h->common.param.i_fps_num / h->common.param.i_fps_den / 2;
            h->common.eprc_t21.cfg.paramH264Cbr.u8MaxQp            = h->common.param.rc.i_qp_max;
            h->common.eprc_t21.cfg.paramH264Cbr.u8MinQp            = h->common.param.rc.i_qp_min;
            h->common.eprc_t21.cfg.paramH264Cbr.u8MaxIQp           = h->common.param.rc.i_qp_max;
            h->common.eprc_t21.cfg.paramH264Cbr.u8MinIQp           = h->common.param.rc.i_qp_min;
            h->common.eprc_t21.cfg.paramH264Cbr.s8IPQpDelta        = 3;
            h->common.eprc_t21.cfg.paramH264Cbr.u8QualityLvl       = 3;
            h->common.eprc_t21.cfg.paramH264Cbr.u8MaxIPQpDelta     = h->common.param.rc.i_gop_qp_step;
            h->common.eprc_t21.cfg.paramH264Cbr.u8MaxPPQpDelta     = h->common.param.rc.i_qp_step;
            h->common.eprc_t21.cfg.paramH264Cbr.s8IQpBias          = h->common.param.rc.i_biaslvl;
            if (is_rcattr_exist) {
                i264e_log(h, C_LOG_INFO, "t21.cfg.rcMode=%u\n",                       h->common.eprc_t21.cfg.rcMode);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cbr.u32Gop=%u\n",           h->common.eprc_t21.cfg.attrH264Cbr.u32Gop);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cbr.u32StatTime=%u\n",      h->common.eprc_t21.cfg.attrH264Cbr.u32StatTime);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cbr.u32SrcFrmRate=%u\n",    h->common.eprc_t21.cfg.attrH264Cbr.u32SrcFrmRate);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cbr.frDstFrmRate=%u\n",     h->common.eprc_t21.cfg.attrH264Cbr.frDstFrmRate);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cbr.u32BitRate=%u\n",       h->common.eprc_t21.cfg.attrH264Cbr.u32BitRate);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cbr.u8FluctuateLevel=%u\n", h->common.eprc_t21.cfg.attrH264Cbr.u8FluctuateLevel);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Cbr.u8MaxIprop=%u\n",      h->common.eprc_t21.cfg.paramH264Cbr.u8MaxIprop);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Cbr.u8MinIprop=%u\n",      h->common.eprc_t21.cfg.paramH264Cbr.u8MinIprop);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Cbr.u8MaxQp=%u\n",         h->common.eprc_t21.cfg.paramH264Cbr.u8MaxQp);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Cbr.u8MinQp=%u\n",         h->common.eprc_t21.cfg.paramH264Cbr.u8MinQp);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Cbr.u8MaxIQp=%u\n",        h->common.eprc_t21.cfg.paramH264Cbr.u8MaxIQp);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Cbr.u8MinIQp=%u\n",        h->common.eprc_t21.cfg.paramH264Cbr.u8MinIQp);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Cbr.s8IPQpDelta=%u\n",     h->common.eprc_t21.cfg.paramH264Cbr.s8IPQpDelta);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Cbr.u8QualityLvl=%u\n",    h->common.eprc_t21.cfg.paramH264Cbr.u8QualityLvl);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Cbr.s8IQpBias=%d\n",       h->common.eprc_t21.cfg.paramH264Cbr.s8IQpBias);
            }
        } else if (h->common.param.rc.i_rc_method == I264E_RC_VBR || h->common.param.rc.i_rc_method == I264E_RC_SMART) {
            if (h->common.param.rc.i_rc_method == I264E_RC_VBR)
                h->common.eprc_t21.cfg.rcMode                      = T21_VBR;
            else
                h->common.eprc_t21.cfg.rcMode                      = T21_SMART;
            h->common.eprc_t21.cfg.attrH264Vbr.u32Gop              = h->common.param.i_gop;
            h->common.eprc_t21.cfg.attrH264Vbr.u32StatTime         = h->common.param.rc.i_static_time;
            h->common.eprc_t21.cfg.attrH264Vbr.u32SrcFrmRate       = h->common.param.i_fps_num;
            h->common.eprc_t21.cfg.attrH264Vbr.frDstFrmRate        = h->common.param.i_fps_den;
            h->common.eprc_t21.cfg.attrH264Vbr.u32MaxBitRate       = h->common.param.rc.i_max_bitrate;
            h->common.eprc_t21.cfg.attrH264Vbr.u8MaxQp             = h->common.param.rc.i_qp_max;
            h->common.eprc_t21.cfg.attrH264Vbr.u8MinQp             = h->common.param.rc.i_qp_min;
            h->common.eprc_t21.cfg.attrH264Vbr.u8MinIQp            = h->common.param.rc.i_qp_min;
            h->common.eprc_t21.cfg.attrH264Vbr.s8AvbrEn            = 1;
            h->common.eprc_t21.cfg.paramH264Vbr.u8ChangePos        = h->common.param.rc.i_change_pos;
            h->common.eprc_t21.cfg.paramH264Vbr.u8MaxIprop         = 100;
            h->common.eprc_t21.cfg.paramH264Vbr.u8MinIprop         = 1;
            h->common.eprc_t21.cfg.paramH264Vbr.u8MaxReEncodeTimes = h->common.param.i_fps_num / h->common.param.i_fps_den / 2;
            h->common.eprc_t21.cfg.paramH264Vbr.s8IPQpDelta        = 3;
            h->common.eprc_t21.cfg.paramH264Vbr.u8MaxIPQpDelta     = h->common.param.rc.i_gop_qp_step;
            h->common.eprc_t21.cfg.paramH264Vbr.u8MaxPPQpDelta     = h->common.param.rc.i_qp_step;
            h->common.eprc_t21.cfg.paramH264Vbr.u8QualityLvl       = h->common.param.rc.i_quality_level;
            h->common.eprc_t21.cfg.paramH264Vbr.s8RQDelta          = 4;
            h->common.eprc_t21.cfg.paramH264Vbr.s8IQpBias          = h->common.param.rc.i_biaslvl;
            if (is_rcattr_exist) {
                i264e_log(h, C_LOG_INFO, "t21.cfg.rcMode=%u\n",                       h->common.eprc_t21.cfg.rcMode);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Vbr.u32Gop=%u\n",           h->common.eprc_t21.cfg.attrH264Vbr.u32Gop);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Vbr.u32StatTime=%u\n",      h->common.eprc_t21.cfg.attrH264Vbr.u32StatTime);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Vbr.u32SrcFrmRate=%u\n",    h->common.eprc_t21.cfg.attrH264Vbr.u32SrcFrmRate);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Vbr.frDstFrmRate=%u\n",     h->common.eprc_t21.cfg.attrH264Vbr.frDstFrmRate);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Vbr.u32MaxBitRate=%u\n",    h->common.eprc_t21.cfg.attrH264Vbr.u32MaxBitRate);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Vbr.u8MaxQp=%u\n",          h->common.eprc_t21.cfg.attrH264Vbr.u8MaxQp);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Vbr.u8MinQp=%u\n",          h->common.eprc_t21.cfg.attrH264Vbr.u8MinQp);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Vbr.u8MinIQp=%u\n",         h->common.eprc_t21.cfg.attrH264Vbr.u8MinIQp);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Vbr.s8AvbrEn=%u\n",         h->common.eprc_t21.cfg.attrH264Vbr.s8AvbrEn);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Vbr.u8ChangePos=%u\n",     h->common.eprc_t21.cfg.paramH264Vbr.u8ChangePos);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Vbr.u8MaxIprop=%u\n",      h->common.eprc_t21.cfg.paramH264Vbr.u8MaxIprop);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Vbr.u8MinIprop=%u\n",      h->common.eprc_t21.cfg.paramH264Vbr.u8MinIprop);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Vbr.u8MaxIPQpDelta=%u\n",  h->common.eprc_t21.cfg.paramH264Vbr.u8MaxIPQpDelta);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Vbr.u8MaxPPQpDelta=%u\n",  h->common.eprc_t21.cfg.paramH264Vbr.u8MaxPPQpDelta);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Vbr.u8QualityLvl=%u\n",    h->common.eprc_t21.cfg.paramH264Vbr.u8QualityLvl);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Vbr.s8RQDelta=%u\n",       h->common.eprc_t21.cfg.paramH264Vbr.s8RQDelta);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Vbr.s8IQpBias=%d\n",       h->common.eprc_t21.cfg.paramH264Vbr.s8IQpBias);
            }
        } else {
            i264e_log(h, C_LOG_ERROR, "%s(%d):Unsupported ratecontrol method:%d\n", __func__, __LINE__, h->common.param.rc.i_rc_method);
            return -1;
        }
		if (!access("/tmp/rc.config", F_OK)) {
			if(i264e_rcfile_config(h)!=0) {
				printf("congfig i264e rcfile failed\r\n");
			}
		}
		if ((initQp >= 0) && (h->common.param.rc.i_rc_method != I264E_RC_CQP)) {
			h->common.eprc_t21.cfg.s8StartQp = initQp;
		} else {
			h->common.eprc_t21.cfg.s8StartQp = -1;
		}
        JZ_VPU_RC_VIDEO_CFG_T21(&h->common.eprc_t21);
    }
    return 0;
}

int i264e_ratecontrolfile_init(i264e_t *h, int initQp)
{
    /*setup para */
    int is_rcattr_exist = !access("/tmp/rcattr", F_OK);
    if ((h->common.param.soc == C_T21) || (h->common.param.soc == C_T23)) {
        h->common.eprc_t21.cfg.encProtocol                     = h->common.reconfig.rcfile.encProtocol;
        h->common.eprc_t21.cfg.gopMode                         = h->common.reconfig.rcfile.gopMode;
        h->common.eprc_t21.cfg.qpgMode                         = h->common.reconfig.rcfile.qpgMode;
        h->common.eprc_t21.cfg.u16FrmWidth                     = h->common.reconfig.rcfile.u16FrmWidth;
        h->common.eprc_t21.cfg.u16FrmHeight                    = h->common.reconfig.rcfile.u16FrmHeight;
        h->common.eprc_t21.cfg.u16MbWidth                      = h->common.reconfig.rcfile.u16MbWidth;
        h->common.eprc_t21.cfg.u16MbHeight                     = h->common.reconfig.rcfile.u16MbHeight;
        h->common.eprc_t21.cfg.fskMode                         = h->common.reconfig.rcfile.fskMode;
        h->common.eprc_t21.cfg.intelMode                       = h->common.reconfig.rcfile.intelMode;
		h->common.eprc_t21.cfg.priority                        = h->common.reconfig.rcfile.priority;
		h->common.eprc_t21.cfg.sceMode                         = h->common.reconfig.rcfile.sceMode;
        h->common.eprc_t21.cfg.superFrmCfg.superFrmMode        = h->common.reconfig.rcfile.superFrmCfg.superFrmMode;
        h->common.eprc_t21.cfg.superFrmCfg.u32SuperIFrmBitsThr = h->common.reconfig.rcfile.superFrmCfg.u32SuperIFrmBitsThr;
        h->common.eprc_t21.cfg.superFrmCfg.u32SuperPFrmBitsThr = h->common.reconfig.rcfile.superFrmCfg.u32SuperPFrmBitsThr;
        h->common.eprc_t21.cfg.superFrmCfg.u32SuperBFrmBitsThr = h->common.reconfig.rcfile.superFrmCfg.u32SuperBFrmBitsThr;
        h->common.eprc_t21.cfg.lostFrmCfg.bFrmLostEn           = h->common.reconfig.rcfile.lostFrmCfg.bFrmLostEn;
        h->common.eprc_t21.cfg.lostFrmCfg.frmLostMode          = h->common.reconfig.rcfile.lostFrmCfg.frmLostMode;
        h->common.eprc_t21.cfg.lostFrmCfg.u32FrmLostBpsThr     = h->common.reconfig.rcfile.lostFrmCfg.u32FrmLostBpsThr;
        h->common.eprc_t21.cfg.lostFrmCfg.u32FrmLostGaps       = h->common.reconfig.rcfile.lostFrmCfg.u32FrmLostGaps;
		h->common.eprc_t21.cfg.s8StartQp					   = h->common.reconfig.rcfile.s8StartQp;
		h->common.eprc_t21.cfg.rcInside.IQpMode                = h->common.reconfig.rcfile.rcInside.IQpMode;
		h->common.eprc_t21.cfg.rcInside.fstPQpMode             = h->common.reconfig.rcfile.rcInside.fstPQpMode;
		h->common.eprc_t21.cfg.rcInside.avgPQpMode             = h->common.reconfig.rcfile.rcInside.avgPQpMode;
		h->common.eprc_t21.cfg.rcInside.qpModel                = h->common.reconfig.rcfile.rcInside.qpModel;
		h->common.eprc_t21.cfg.rcInside.bitrLvlEn              = h->common.reconfig.rcfile.rcInside.bitrLvlEn;
		h->common.eprc_t21.cfg.rcInside.sceJudgeLvl            = h->common.reconfig.rcfile.rcInside.sceJudgeLvl;
		h->common.eprc_t21.cfg.rcInside.sceJudgeEn             = h->common.reconfig.rcfile.rcInside.sceJudgeEn;
		h->common.eprc_t21.cfg.rcInside.useFstPEn              = h->common.reconfig.rcfile.rcInside.qpFluctLvl;
		h->common.eprc_t21.cfg.rcInside.avgQpNewEn             = h->common.reconfig.rcfile.rcInside.avgQpNewEn;
		h->common.eprc_t21.cfg.rcInside.avgPQpNum              = h->common.reconfig.rcfile.rcInside.avgPQpNum;
		h->common.eprc_t21.cfg.rcInside.qpFluctEn              = h->common.reconfig.rcfile.rcInside.qpFluctEn;
		h->common.eprc_t21.cfg.rcInside.qpFluctLvl             = h->common.reconfig.rcfile.rcInside.qpFluctLvl;
		h->common.eprc_t21.cfg.rcInside.fluctLvl               = h->common.reconfig.rcfile.rcInside.fluctLvl;
		h->common.eprc_t21.cfg.rcInside.bndEn                  = h->common.reconfig.rcfile.rcInside.bndEn;
		h->common.eprc_t21.cfg.rcInside.BndLvl                 = h->common.reconfig.rcfile.rcInside.BndLvl;
		h->common.eprc_t21.cfg.rcInside.stmLvl                 = h->common.reconfig.rcfile.rcInside.stmLvl;
		h->common.eprc_t21.cfg.rcInside.t0Method               = h->common.reconfig.rcfile.rcInside.t0Method;
		h->common.eprc_t21.cfg.rcInside.avgPBitsNum            = h->common.reconfig.rcfile.rcInside.avgPBitsNum;
		h->common.eprc_t21.cfg.rcInside.gammaEn                = h->common.reconfig.rcfile.rcInside.gammaEn;
		h->common.eprc_t21.cfg.rcInside.stillSize              = h->common.reconfig.rcfile.rcInside.stillSize;
		h->common.eprc_t21.cfg.rcInside.moveSize               = h->common.reconfig.rcfile.rcInside.moveSize;
		h->common.eprc_t21.cfg.rcInside.qpgIMode               = h->common.reconfig.rcfile.rcInside.qpgIMode;
		h->common.eprc_t21.cfg.rcInside.qpgPMode               = h->common.reconfig.rcfile.rcInside.qpgPMode;
        h->common.eprc_t21.cfg.siParamH264.frm_re0A            = h->common.reconfig.rcfile.siParamH264.frm_re0A;
        h->common.eprc_t21.cfg.siParamH264.frm_re1A            = h->common.reconfig.rcfile.siParamH264.frm_re1A;
        h->common.eprc_t21.cfg.siParamH264.frm_re2A            = h->common.reconfig.rcfile.siParamH264.frm_re2A;
        h->common.eprc_t21.cfg.siParamH264.frm_re3A            = h->common.reconfig.rcfile.siParamH264.frm_re3A;
        h->common.eprc_t21.cfg.siParamH264.scl                 = h->common.reconfig.rcfile.siParamH264.scl;
        h->common.eprc_t21.cfg.siParamH264.hpel_en             = h->common.reconfig.rcfile.siParamH264.hpel_en;
        h->common.eprc_t21.cfg.siParamH264.qpel_en             = h->common.reconfig.rcfile.siParamH264.qpel_en;
        h->common.eprc_t21.cfg.siParamH264.max_sech_step_i     = h->common.reconfig.rcfile.siParamH264.max_sech_step_i;
        h->common.eprc_t21.cfg.siParamH264.max_mvrx_i          = h->common.reconfig.rcfile.siParamH264.max_mvrx_i;
        h->common.eprc_t21.cfg.siParamH264.max_mvry_i          = h->common.reconfig.rcfile.siParamH264.max_mvry_i;
        h->common.eprc_t21.cfg.siParamH264.fs_en               = h->common.reconfig.rcfile.siParamH264.fs_en;
        h->common.eprc_t21.cfg.siParamH264.fs_md               = h->common.reconfig.rcfile.siParamH264.fs_md;
        h->common.eprc_t21.cfg.siParamH264.fs_px               = h->common.reconfig.rcfile.siParamH264.fs_px;
        h->common.eprc_t21.cfg.siParamH264.fs_py               = h->common.reconfig.rcfile.siParamH264.fs_py;
        h->common.eprc_t21.cfg.siParamH264.fs_rx               = h->common.reconfig.rcfile.siParamH264.fs_rx;
        h->common.eprc_t21.cfg.siParamH264.fs_ry               = h->common.reconfig.rcfile.siParamH264.fs_ry;
        h->common.eprc_t21.cfg.siParamH264.frm_mv_en           = h->common.reconfig.rcfile.siParamH264.frm_mv_en;
        h->common.eprc_t21.cfg.siParamH264.frm_mv_size         = h->common.reconfig.rcfile.siParamH264.frm_mv_size;
        h->common.eprc_t21.cfg.siParamH264.glb_mv_en           = h->common.reconfig.rcfile.siParamH264.glb_mv_en;
        h->common.eprc_t21.cfg.siParamH264.me_step_en          = h->common.reconfig.rcfile.siParamH264.me_step_en;
        h->common.eprc_t21.cfg.siParamH264.me_step_0           = h->common.reconfig.rcfile.siParamH264.me_step_0;
        h->common.eprc_t21.cfg.siParamH264.me_step_1           = h->common.reconfig.rcfile.siParamH264.me_step_1;
        h->common.eprc_t21.cfg.siParamH264.deadzone0Aiy4       = h->common.reconfig.rcfile.siParamH264.deadzone0Aiy4;
        h->common.eprc_t21.cfg.siParamH264.deadzone1Apy4       = h->common.reconfig.rcfile.siParamH264.deadzone1Apy4;
        h->common.eprc_t21.cfg.siParamH264.deadzone2Aic4       = h->common.reconfig.rcfile.siParamH264.deadzone2Aic4;
        h->common.eprc_t21.cfg.siParamH264.deadzone3Apc4       = h->common.reconfig.rcfile.siParamH264.deadzone3Apc4;
        h->common.eprc_t21.cfg.siParamH264.deadzone4Aiy8       = h->common.reconfig.rcfile.siParamH264.deadzone4Aiy8;
        h->common.eprc_t21.cfg.siParamH264.deadzone5Apy8       = h->common.reconfig.rcfile.siParamH264.deadzone5Apy8;
        h->common.eprc_t21.cfg.siParamH264.deadzone5Apy8       = h->common.reconfig.rcfile.siParamH264.deadzone5Apy8;
        h->common.eprc_t21.cfg.siParamH264.deadzone7Aicdc      = h->common.reconfig.rcfile.siParamH264.deadzone7Aicdc;
        h->common.eprc_t21.cfg.siParamH264.deadzone8Apcdc      = h->common.reconfig.rcfile.siParamH264.deadzone8Apcdc;
        h->common.eprc_t21.cfg.siParamH264.acmask_mode         = h->common.reconfig.rcfile.siParamH264.acmask_mode;
        h->common.eprc_t21.cfg.siParamH264.deblock             = h->common.reconfig.rcfile.siParamH264.deblock;
        h->common.eprc_t21.cfg.siParamH264.alpha_c0_offset     = h->common.reconfig.rcfile.siParamH264.alpha_c0_offset;
        h->common.eprc_t21.cfg.siParamH264.beta_offset         = h->common.reconfig.rcfile.siParamH264.beta_offset;
        h->common.eprc_t21.cfg.siParamH264.jm_lambda2_en       = h->common.reconfig.rcfile.siParamH264.jm_lambda2_en;
        h->common.eprc_t21.cfg.siParamH264.inter_nei_en        = h->common.reconfig.rcfile.siParamH264.inter_nei_en;
        h->common.eprc_t21.cfg.siParamH264.skip_bias_en        = h->common.reconfig.rcfile.siParamH264.skip_bias_en;
        h->common.eprc_t21.cfg.siParamH264.dcm_en              = h->common.reconfig.rcfile.siParamH264.dcm_en;
        h->common.eprc_t21.cfg.siParamH264.dcm_param           = h->common.reconfig.rcfile.siParamH264.dcm_param;
        h->common.eprc_t21.cfg.siParamH264.use_intra_in_pframe = h->common.reconfig.rcfile.siParamH264.use_intra_in_pframe;
        h->common.eprc_t21.cfg.siParamH264.cqp_offset          = h->common.reconfig.rcfile.siParamH264.cqp_offset;
        h->common.eprc_t21.cfg.siParamH264.mb_mode_useMASK     = h->common.reconfig.rcfile.siParamH264.mb_mode_useMASK;
        h->common.eprc_t21.cfg.siParamH264.sas_eigen_en        = h->common.reconfig.rcfile.siParamH264.sas_eigen_en;
        h->common.eprc_t21.cfg.siParamH264.crp_eigen_en        = h->common.reconfig.rcfile.siParamH264.crp_eigen_en;
        h->common.eprc_t21.cfg.siParamH264.refresh_enMASK      = h->common.reconfig.rcfile.siParamH264.refresh_enMASK;
        h->common.eprc_t21.cfg.siParamH264.refresh_mode        = h->common.reconfig.rcfile.siParamH264.refresh_mode;
        h->common.eprc_t21.cfg.siParamH264.refresh_bias        = h->common.reconfig.rcfile.siParamH264.refresh_bias;
        h->common.eprc_t21.cfg.siParamH264.refresh_cplx_thd    = h->common.reconfig.rcfile.siParamH264.refresh_cplx_thd;
        h->common.eprc_t21.cfg.siParamH264.force_i16dc         = h->common.reconfig.rcfile.siParamH264.force_i16dc;
        h->common.eprc_t21.cfg.siParamH264.i16dc_cplx_thdIDR   = h->common.reconfig.rcfile.siParamH264.i16dc_cplx_thdIDR;
        h->common.eprc_t21.cfg.siParamH264.i16dc_cplx_thdELSE  = h->common.reconfig.rcfile.siParamH264.i16dc_cplx_thdELSE;
        h->common.eprc_t21.cfg.siParamH264.i16dc_qp_base       = h->common.reconfig.rcfile.siParamH264.i16dc_qp_base;
        h->common.eprc_t21.cfg.siParamH264.i16dc_qp_sel        = h->common.reconfig.rcfile.siParamH264.i16dc_qp_sel;
        h->common.eprc_t21.cfg.siParamH264.force_i16           = h->common.reconfig.rcfile.siParamH264.force_i16;
        h->common.eprc_t21.cfg.siParamH264.i16_qp_baseIDR      = h->common.reconfig.rcfile.siParamH264.i16_qp_baseIDR;
        h->common.eprc_t21.cfg.siParamH264.i16_qp_baseELSE     = h->common.reconfig.rcfile.siParamH264.i16_qp_baseELSE;
        h->common.eprc_t21.cfg.siParamH264.i16_qp_sel          = h->common.reconfig.rcfile.siParamH264.i16_qp_sel;
        h->common.eprc_t21.cfg.siParamH264.rrs_enMASK          = h->common.reconfig.rcfile.siParamH264.rrs_enMASK;
        h->common.eprc_t21.cfg.siParamH264.rrs_uv_enMASK       = h->common.reconfig.rcfile.siParamH264.rrs_uv_enMASK;
        h->common.eprc_t21.cfg.siParamH264.rrs_size_y          = h->common.reconfig.rcfile.siParamH264.rrs_size_y;
        h->common.eprc_t21.cfg.siParamH264.rrs_size_c          = h->common.reconfig.rcfile.siParamH264.rrs_size_c;
        h->common.eprc_t21.cfg.siParamH264.skin_dt_en          = h->common.reconfig.rcfile.siParamH264.skin_dt_en;
        h->common.eprc_t21.cfg.siParamH264.skin_lvl            = h->common.reconfig.rcfile.siParamH264.skin_lvl;
        h->common.eprc_t21.cfg.siParamH264.skin_cnt_thd        = h->common.reconfig.rcfile.siParamH264.skin_cnt_thd;

        switch(h->common.eprc_t21.cfg.gopMode) {
        case NORMALP:
            h->common.eprc_t21.cfg.gopNormalP.s8IPQpDelta  = h->common.reconfig.rcfile.gopNormalP.s8IPQpDelta;
            break;
        case SMARTP:
            h->common.eprc_t21.cfg.gopSmartP.u32BgInterval = h->common.reconfig.rcfile.gopSmartP.u32BgInterval;
            h->common.eprc_t21.cfg.gopSmartP.s8BgQpDelta   = h->common.reconfig.rcfile.gopSmartP.s8BgQpDelta;
            h->common.eprc_t21.cfg.gopSmartP.s8ViQpDelta   = h->common.reconfig.rcfile.gopSmartP.s8ViQpDelta;
            break;
        case DOULP:
            h->common.eprc_t21.cfg.gopDoulP.u32SPInterval  = h->common.reconfig.rcfile.gopDoulP.u32SPInterval;
            h->common.eprc_t21.cfg.gopDoulP.s8SPQpDelta    = h->common.reconfig.rcfile.gopDoulP.s8SPQpDelta;
            h->common.eprc_t21.cfg.gopDoulP.s8IPQpDelta    = h->common.reconfig.rcfile.gopDoulP.s8IPQpDelta;
            break;
        case BIPREDP:
            h->common.eprc_t21.cfg.gopBipredP.u32BFrmNum   = h->common.reconfig.rcfile.gopBipredP.u32BFrmNum;
            h->common.eprc_t21.cfg.gopBipredP.s8BQpDelta   = h->common.reconfig.rcfile.gopBipredP.s8BQpDelta;
            h->common.eprc_t21.cfg.gopBipredP.s8IPQpDelta  = h->common.reconfig.rcfile.gopBipredP.s8IPQpDelta;
            break;
        default:
            break;
        }
        if (h->common.reconfig.rcfile.rcMode == I264E_RC_CQP) {
            h->common.eprc_t21.cfg.rcMode                    = h->common.reconfig.rcfile.rcMode;
            h->common.eprc_t21.cfg.attrH264Cqp.u32Gop        = h->common.reconfig.rcfile.attrH264Cqp.u32Gop;
            h->common.eprc_t21.cfg.attrH264Cqp.u32SrcFrmRate = h->common.reconfig.rcfile.attrH264Cqp.u32SrcFrmRate;
            h->common.eprc_t21.cfg.attrH264Cqp.frDstFrmRate  = h->common.reconfig.rcfile.attrH264Cqp.frDstFrmRate;
            h->common.eprc_t21.cfg.attrH264Cqp.u8IQp         = h->common.reconfig.rcfile.attrH264Cqp.u8IQp;
            h->common.eprc_t21.cfg.attrH264Cqp.u8PQp         = h->common.reconfig.rcfile.attrH264Cqp.u8PQp;
            if (is_rcattr_exist) {
                i264e_log(h, C_LOG_INFO, "t21.cfg.rcMode=%u\n",                    h->common.eprc_t21.cfg.rcMode);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cqp.u32Gop=%u\n",        h->common.eprc_t21.cfg.attrH264Cqp.u32Gop);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cqp.u32SrcFrmRate=%u\n", h->common.eprc_t21.cfg.attrH264Cqp.u32SrcFrmRate);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cqp.frDstFrmRate=%u\n",  h->common.eprc_t21.cfg.attrH264Cqp.frDstFrmRate);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cqp.u8IQp=%u\n",         h->common.eprc_t21.cfg.attrH264Cqp.u8IQp);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cqp.u8PQp=%u\n",         h->common.eprc_t21.cfg.attrH264Cqp.u8PQp);
            }
        }
        else if (h->common.reconfig.rcfile.rcMode == I264E_RC_CBR) {
            h->common.eprc_t21.cfg.rcMode                          = h->common.reconfig.rcfile.rcMode;
            h->common.eprc_t21.cfg.attrH264Cbr.u32Gop              = h->common.reconfig.rcfile.attrH264Cbr.u32Gop;
            h->common.eprc_t21.cfg.attrH264Cbr.u32StatTime         = h->common.reconfig.rcfile.attrH264Cbr.u32StatTime;
            h->common.eprc_t21.cfg.attrH264Cbr.u32SrcFrmRate       = h->common.reconfig.rcfile.attrH264Cbr.u32SrcFrmRate;
            h->common.eprc_t21.cfg.attrH264Cbr.frDstFrmRate        = h->common.reconfig.rcfile.attrH264Cbr.frDstFrmRate;
            h->common.eprc_t21.cfg.attrH264Cbr.u32BitRate          = h->common.reconfig.rcfile.attrH264Cbr.u32BitRate;
            h->common.eprc_t21.cfg.attrH264Cbr.u8FluctuateLevel    = h->common.reconfig.rcfile.attrH264Cbr.u8FluctuateLevel;
            h->common.eprc_t21.cfg.paramH264Cbr.u8MaxIprop         = h->common.reconfig.rcfile.paramH264Cbr.u8MaxIprop;
            h->common.eprc_t21.cfg.paramH264Cbr.u8MinIprop         = h->common.reconfig.rcfile.paramH264Cbr.u8MinIprop;
            h->common.eprc_t21.cfg.paramH264Cbr.u8MaxReEncodeTimes = h->common.reconfig.rcfile.paramH264Cbr.u8MaxReEncodeTimes;
            h->common.eprc_t21.cfg.paramH264Cbr.u8MaxQp            = h->common.reconfig.rcfile.paramH264Cbr.u8MaxQp;
            h->common.eprc_t21.cfg.paramH264Cbr.u8MinQp            = h->common.reconfig.rcfile.paramH264Cbr.u8MinQp;
            h->common.eprc_t21.cfg.paramH264Cbr.u8MaxIQp           = h->common.reconfig.rcfile.paramH264Cbr.u8MaxIQp;
            h->common.eprc_t21.cfg.paramH264Cbr.u8MinIQp           = h->common.reconfig.rcfile.paramH264Cbr.u8MinIQp;
            h->common.eprc_t21.cfg.paramH264Cbr.s8IPQpDelta        = h->common.reconfig.rcfile.paramH264Cbr.s8IPQpDelta;
            h->common.eprc_t21.cfg.paramH264Cbr.u8QualityLvl       = h->common.reconfig.rcfile.paramH264Cbr.u8QualityLvl;
            h->common.eprc_t21.cfg.paramH264Cbr.u8MaxIPQpDelta     = h->common.reconfig.rcfile.paramH264Cbr.u8MaxIPQpDelta;
            h->common.eprc_t21.cfg.paramH264Cbr.u8MaxPPQpDelta     = h->common.reconfig.rcfile.paramH264Cbr.u8MaxPPQpDelta;
            h->common.eprc_t21.cfg.paramH264Cbr.s8IQpBias          = h->common.reconfig.rcfile.paramH264Cbr.s8IQpBias;
            if (is_rcattr_exist) {
                i264e_log(h, C_LOG_INFO, "t21.cfg.rcMode=%u\n",                       h->common.eprc_t21.cfg.rcMode);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cbr.u32Gop=%u\n",           h->common.eprc_t21.cfg.attrH264Cbr.u32Gop);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cbr.u32StatTime=%u\n",      h->common.eprc_t21.cfg.attrH264Cbr.u32StatTime);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cbr.u32SrcFrmRate=%u\n",    h->common.eprc_t21.cfg.attrH264Cbr.u32SrcFrmRate);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cbr.frDstFrmRate=%u\n",     h->common.eprc_t21.cfg.attrH264Cbr.frDstFrmRate);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cbr.u32BitRate=%u\n",       h->common.eprc_t21.cfg.attrH264Cbr.u32BitRate);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Cbr.u8FluctuateLevel=%u\n", h->common.eprc_t21.cfg.attrH264Cbr.u8FluctuateLevel);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Cbr.u8MaxIprop=%u\n",      h->common.eprc_t21.cfg.paramH264Cbr.u8MaxIprop);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Cbr.u8MinIprop=%u\n",      h->common.eprc_t21.cfg.paramH264Cbr.u8MinIprop);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Cbr.u8MaxQp=%u\n",         h->common.eprc_t21.cfg.paramH264Cbr.u8MaxQp);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Cbr.u8MinQp=%u\n",         h->common.eprc_t21.cfg.paramH264Cbr.u8MinQp);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Cbr.u8MaxIQp=%u\n",        h->common.eprc_t21.cfg.paramH264Cbr.u8MaxIQp);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Cbr.u8MinIQp=%u\n",        h->common.eprc_t21.cfg.paramH264Cbr.u8MinIQp);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Cbr.s8IPQpDelta=%u\n",     h->common.eprc_t21.cfg.paramH264Cbr.s8IPQpDelta);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Cbr.u8QualityLvl=%u\n",    h->common.eprc_t21.cfg.paramH264Cbr.u8QualityLvl);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Cbr.s8IQpBias=%d\n",       h->common.eprc_t21.cfg.paramH264Cbr.s8IQpBias);
            }
        } else if (h->common.reconfig.rcfile.rcMode == I264E_RC_VBR || h->common.reconfig.rcfile.rcMode == I264E_RC_SMART) {
            h->common.eprc_t21.cfg.rcMode                          = h->common.reconfig.rcfile.rcMode;
            h->common.eprc_t21.cfg.attrH264Vbr.u32Gop              = h->common.reconfig.rcfile.attrH264Vbr.u32Gop;
            h->common.eprc_t21.cfg.attrH264Vbr.u32StatTime         = h->common.reconfig.rcfile.attrH264Vbr.u32StatTime;
            h->common.eprc_t21.cfg.attrH264Vbr.u32SrcFrmRate       = h->common.reconfig.rcfile.attrH264Vbr.u32SrcFrmRate;
            h->common.eprc_t21.cfg.attrH264Vbr.frDstFrmRate        = h->common.reconfig.rcfile.attrH264Vbr.frDstFrmRate;
            h->common.eprc_t21.cfg.attrH264Vbr.u32MaxBitRate       = h->common.reconfig.rcfile.attrH264Vbr.u32MaxBitRate;
            h->common.eprc_t21.cfg.attrH264Vbr.u8MaxQp             = h->common.reconfig.rcfile.attrH264Vbr.u8MaxQp;
            h->common.eprc_t21.cfg.attrH264Vbr.u8MinQp             = h->common.reconfig.rcfile.attrH264Vbr.u8MinQp;
            h->common.eprc_t21.cfg.attrH264Vbr.u8MinIQp            = h->common.reconfig.rcfile.attrH264Vbr.u8MinIQp;
            h->common.eprc_t21.cfg.attrH264Vbr.s8AvbrEn            = h->common.reconfig.rcfile.attrH264Vbr.s8AvbrEn;
            h->common.eprc_t21.cfg.paramH264Vbr.u8ChangePos        = h->common.reconfig.rcfile.paramH264Vbr.u8ChangePos;
            h->common.eprc_t21.cfg.paramH264Vbr.u8MaxIprop         = h->common.reconfig.rcfile.paramH264Vbr.u8MaxIprop;
            h->common.eprc_t21.cfg.paramH264Vbr.u8MinIprop         = h->common.reconfig.rcfile.paramH264Vbr.u8MinIprop;
            h->common.eprc_t21.cfg.paramH264Vbr.u8MaxReEncodeTimes = h->common.reconfig.rcfile.paramH264Vbr.u8MaxReEncodeTimes;
            h->common.eprc_t21.cfg.paramH264Vbr.s8IPQpDelta        = h->common.reconfig.rcfile.paramH264Vbr.s8IPQpDelta;
            h->common.eprc_t21.cfg.paramH264Vbr.u8MaxIPQpDelta     = h->common.reconfig.rcfile.paramH264Vbr.u8MaxIPQpDelta;
            h->common.eprc_t21.cfg.paramH264Vbr.u8MaxPPQpDelta     = h->common.reconfig.rcfile.paramH264Vbr.u8MaxPPQpDelta;
            h->common.eprc_t21.cfg.paramH264Vbr.u8QualityLvl       = h->common.reconfig.rcfile.paramH264Vbr.u8QualityLvl;
            h->common.eprc_t21.cfg.paramH264Vbr.s8RQDelta          = h->common.reconfig.rcfile.paramH264Vbr.s8RQDelta;
            h->common.eprc_t21.cfg.paramH264Vbr.s8IQpBias          = h->common.reconfig.rcfile.paramH264Vbr.s8IQpBias;
            if (is_rcattr_exist) {
                i264e_log(h, C_LOG_INFO, "t21.cfg.rcMode=%u\n",                       h->common.eprc_t21.cfg.rcMode);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Vbr.u32Gop=%u\n",           h->common.eprc_t21.cfg.attrH264Vbr.u32Gop);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Vbr.u32StatTime=%u\n",      h->common.eprc_t21.cfg.attrH264Vbr.u32StatTime);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Vbr.u32SrcFrmRate=%u\n",    h->common.eprc_t21.cfg.attrH264Vbr.u32SrcFrmRate);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Vbr.frDstFrmRate=%u\n",     h->common.eprc_t21.cfg.attrH264Vbr.frDstFrmRate);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Vbr.u32MaxBitRate=%u\n",    h->common.eprc_t21.cfg.attrH264Vbr.u32MaxBitRate);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Vbr.u8MaxQp=%u\n",          h->common.eprc_t21.cfg.attrH264Vbr.u8MaxQp);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Vbr.u8MinQp=%u\n",          h->common.eprc_t21.cfg.attrH264Vbr.u8MinQp);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Vbr.u8MinIQp=%u\n",         h->common.eprc_t21.cfg.attrH264Vbr.u8MinIQp);
                i264e_log(h, C_LOG_INFO, "t21.cfg.attrH264Vbr.s8AvbrEn=%u\n",         h->common.eprc_t21.cfg.attrH264Vbr.s8AvbrEn);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Vbr.u8ChangePos=%u\n",     h->common.eprc_t21.cfg.paramH264Vbr.u8ChangePos);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Vbr.u8MaxIprop=%u\n",      h->common.eprc_t21.cfg.paramH264Vbr.u8MaxIprop);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Vbr.u8MinIprop=%u\n",      h->common.eprc_t21.cfg.paramH264Vbr.u8MinIprop);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Vbr.u8MaxIPQpDelta=%u\n",  h->common.eprc_t21.cfg.paramH264Vbr.u8MaxIPQpDelta);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Vbr.u8MaxPPQpDelta=%u\n",  h->common.eprc_t21.cfg.paramH264Vbr.u8MaxPPQpDelta);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Vbr.u8QualityLvl=%u\n",    h->common.eprc_t21.cfg.paramH264Vbr.u8QualityLvl);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Vbr.s8RQDelta=%u\n",       h->common.eprc_t21.cfg.paramH264Vbr.s8RQDelta);
                i264e_log(h, C_LOG_INFO, "t21.cfg.paramH264Vbr.s8IQpBias=%d\n",       h->common.eprc_t21.cfg.paramH264Vbr.s8IQpBias);
            }
        } else {
            i264e_log(h, C_LOG_ERROR, "%s(%d):Unsupported ratecontrol method:%d\n", __func__, __LINE__, h->common.param.rc.i_rc_method);
            return -1;
        }
		if ((initQp >= 0) && (h->common.param.rc.i_rc_method != I264E_RC_CQP)) {
			h->common.eprc_t21.cfg.s8StartQp = initQp;
		} else {
			h->common.eprc_t21.cfg.s8StartQp = -1;
		}
        JZ_VPU_RC_VIDEO_CFG_T21(&h->common.eprc_t21);
    }
    return 0;
}

int i264e_ratecontrol_start(i264e_thread_t *t)
{
    int bIFrmReq = ((t->ip.i_frame % t->h->common.param.i_gop) == 0) ? 1 : 0;

    if ((t->h->common.param.soc == C_T21) || (t->h->common.param.soc == C_T23)) {
        t->h->common.eprc_t21.h264SliceInfo = (H264E_SLICE_INFO_S_T21*)t->p.icodec_io.protocol.h264e.H264E_SliceInfo;
        t->h->common.eprc_t21.vpuPri = t->h->thread.p.vpupri;
        t->h->common.eprc_t21.ncuEn = t->p.fenc->b_use_ncu;
		t->h->common.eprc_t21.aezoneEn = t->p.fenc->b_use_aezone;
        t->h->common.eprc_t21.fenc[0]    = t->p.fenc->plane[0];
        t->h->common.eprc_t21.fenc[1]    = t->p.fenc->plane[1];
        t->h->common.eprc_t21.stride[0]   = t->p.fenc->i_stride[0];
        t->h->common.eprc_t21.stride[1]   = t->p.fenc->i_stride[1];
        if (t->p.fenc->b_use_ncu) {
            t->h->common.eprc_t21.ncuInfo.yWidth   = t->p.fenc->ncu_frame_info->width;
            t->h->common.eprc_t21.ncuInfo.yHeight  = t->p.fenc->ncu_frame_info->height;
            t->h->common.eprc_t21.ncuInfo.yfrmNum  = t->p.fenc->ncu_frame_info->frm_num;
            t->h->common.eprc_t21.ncuInfo.yBlkSize = t->p.fenc->ncu_frame_info->sta_y_block_size;
            t->h->common.eprc_t21.ncuInfo.yBufSize = t->p.fenc->ncu_frame_info->sta_y_buf_size;
            t->h->common.eprc_t21.ncuInfo.yStride  = t->p.fenc->ncu_frame_info->sta_y_stride;
            t->h->common.eprc_t21.ncuInfo.yBuf     = t->p.fenc->ncu_frame_info->sta_y_buf;
        }
        else{
            t->h->common.eprc_t21.ncuInfo.yBuf = NULL;
        }
		if (t->h->common.eprc_t21.aezoneEn) {
			memcpy(&(t->h->common.eprc_t21.ae_zone), t->p.fenc->ae_zone_info, sizeof(RC_AE_ZONE_INFO_S));
		}
		if ((t->h->common.param.rc.i_rc_method >= I264E_RC_CQP) || (t->h->common.param.rc.i_rc_method <= I264E_RC_SMART)) {

            if (bIFrmReq == 1 && t->ip.i_frame != 0)
                bIFrmReq = 0;
            t->h->common.eprc_t21.bIFrmReq = bIFrmReq;
            i264e_rdposidx_t *rdpc = &t->h->common.skip_h.rdposidx_cur;
            i264e_skip_info_t *skip_info = &t->h->common.skip_h.skip_info;
            t->h->common.eprc_t21.cfg.gopMode  = skip_info->skip_type;
            t->h->common.eprc_t21.frmSkipType = rdpc->fsktype;

            JZ_VPU_RC_FRAME_START_T21(&t->h->common.eprc_t21);
            //t->h->common.pps->b_transform_8x8_mode = t->h->common.eprc_t21.sliceInfo.dct8x8_en;
            if ((bIFrmReq && ((int)t->h->common.eprc_t21.frmType != (int)I264E_SLICE_TYPE_I))
                || ((bIFrmReq == 0) && ((int)t->h->common.eprc_t21.frmType != (int)I264E_SLICE_TYPE_P))) {
                i264e_log(t->h, C_LOG_ERROR, "line:%d, unmatched bIFrmReq(%d) and frameType(%d)\n",
                          __LINE__, bIFrmReq, t->h->common.eprc_t21.frmType);
                return -1;
            }
            t->p.i_global_qp =  t->h->common.eprc_t21.u8FrmQp;
        }
    }
            return 0;
}

#define SOFT_CAL_MAD
#ifdef SOFT_CAL_MAD
static int cal_p_mad(i264e_thread_t *t)
{
    pixel *d0 = t->p.fenc->plane[0];
    pixel *d1 = t->p.fref->plane[0];
    int mad = 0, i, j;
    int w = t->p.fenc->i_width[0];
    int h = t->p.fenc->i_lines[0];
    int s0 = t->p.fenc->i_stride[0];
    int s1 = t->p.fref->i_stride[0];
    pixel *p0, *p1;

    for (i = 0; i < h; i++) {
        p0 = d0 + i * s0;
        p1 = d1 + i * s1;
        for (j = 0; j < w; j++) {
            mad += abs(p0[j] - p1[j]);
        }
    }

    return mad;
}
#endif

int i264e_ratecontrol_end(i264e_thread_t *t)
{
    i264e_rc_middata_t *prc_middata = &t->p.fdec->rc_middata;
    if ((t->h->common.param.rc.i_rc_method >= I264E_RC_CQP) || (t->h->common.param.rc.i_rc_method <= I264E_RC_SMART)) {
        if((t->h->common.param.soc == C_T21) || (t->h->common.param.soc == C_T23)) {
            unsigned int *mb_info = t->p.icodec_io.protocol.h264e.priv;
            // prc_middata->pred_mad = t->p.icodec_io.protocol.h264e.cmpx; //Read from VPU hardware
            prc_middata->mv_range[0] = mb_info[1] & 0xffffffff;
            prc_middata->mv_range[1] = mb_info[3] & 0xffffffff;
            prc_middata->mv_range[2] = mb_info[5] & 0xffffffff;
            prc_middata->mv_range[3] = mb_info[7] & 0xffffffff;
            prc_middata->mv_range[4] = mb_info[9] & 0xffffffff;
            prc_middata->mv_range[5] = mb_info[11] & 0xffffffff;
            prc_middata->mv_range[6] = mb_info[13] & 0xffffffff;
            prc_middata->mv_range[7] = mb_info[15] & 0xffffffff;
            prc_middata->intra_weight[0] = (mb_info[17] & 0xffff) + (mb_info[19] & 0xffff0000);
            prc_middata->intra_weight[1] = (mb_info[21] & 0xffff) + (mb_info[23] & 0xffff0000);
            prc_middata->glb_mvx_sum  = mb_info[25] & 0xffffffff;
            prc_middata->glb_mvy_sum  = mb_info[27] & 0xffffffff;
            prc_middata->glb_mv_cnt  = mb_info[29] & 0xffffffff;
            prc_middata->pred_mad = mb_info[31] & 0xffffffff;
            prc_middata->qpg_rgnc[0] = mb_info[33] & 0xffffffff;
            prc_middata->qpg_rgnc[1] = mb_info[35] & 0xffffffff;
            prc_middata->qpg_rgnc[2] = mb_info[37] & 0xffffffff;
            prc_middata->qpg_rgnc[3] = mb_info[39] & 0xffffffff;
            prc_middata->mb_inter_mv = (mb_info[41] & 0x7ffffff) + (mb_info[43] & 0x7ffffff);  //Read from VPU hardware
            prc_middata->mb_inter_cnt = (mb_info[45] & 0x3fff) + ((mb_info[43] >> 16)& 0x3fff);  //Read from VPU hardware
            prc_middata->frm_i16Cnt = (mb_info[47] >> 0) & 0x7FFF;
            prc_middata->frm_i4Cnt  = (mb_info[47] >> 16) & 0x7FFF;
            prc_middata->frm_i8Cnt  = (mb_info[49] >> 0) & 0x7FFF;
            //bak step: get value
#if 0
            prc_middata->frmLink.cfg_blk_wid = mb_info[25 * 2 + 1] & 0xF;
            prc_middata->frmLink.cfg_blk_hei = (mb_info[25 * 2 + 1] >> 4) & 0xF;
            prc_middata->frmLink.cfg_blk_i_thr = (mb_info[25 * 2 + 1] >> 8) & 0x1FF;
            prc_middata->frmLink.cfg_blk_mvr_thr1 = (mb_info[26 * 2 + 1] >> 0) & 0x7FFF;
            prc_middata->frmLink.cfg_blk_mvr_thr2 = (mb_info[26 * 2 + 1] >> 15) & 0x1FFFF;
            prc_middata->frmLink.cfg_blk_mvr_thr3 = (mb_info[27 * 2 + 1] >> 0) & 0xFFFFF;
            prc_middata->frmLink.blk_mvr0 = mb_info[0 * 2 + 1] & 0xFFFFFFFF;
            prc_middata->frmLink.blk_mvr1 = mb_info[1 * 2 + 1] & 0xFFFFFFFF;
            prc_middata->frmLink.blk_mvr2 = mb_info[2 * 2 + 1] & 0xFFFFFFFF;
            prc_middata->frmLink.blk_mvr3 = mb_info[3 * 2 + 1] & 0xFFFFFFFF;
            prc_middata->frmLink.blk_mvr4 = mb_info[4 * 2 + 1] & 0xFFFFFFFF;
            prc_middata->frmLink.blk_mvr5 = mb_info[5 * 2 + 1] & 0xFFFFFFFF;
            prc_middata->frmLink.blk_mvr6 = mb_info[6 * 2 + 1] & 0xFFFFFFFF;
            prc_middata->frmLink.blk_mvr7 = mb_info[7 * 2 + 1] & 0xFFFFFFFF;
            prc_middata->frmLink.blk_iw0 = mb_info[8 * 2 + 1] & 0xFFFFFFFF;
            prc_middata->frmLink.blk_iw1 = mb_info[9 * 2 + 1] & 0xFFFFFFFF;
            prc_middata->frmLink.blk_iw2 = mb_info[10 * 2 + 1] & 0xFFFFFFFF;
            prc_middata->frmLink.blk_iw3 = mb_info[11 * 2 + 1] & 0xFFFFFFFF;
            prc_middata->frmLink.cfg_info_en = (mb_info[28 * 2 + 1] >> 9) & 1;
            prc_middata->frmLink.cfg_mvd_all = (mb_info[28 * 2 + 1] >> 12) & 1;
            prc_middata->frmLink.cfg_mvd_abs = (mb_info[28 * 2 + 1] >> 13) & 1;
            prc_middata->frmLink.cfg_mv_all = (mb_info[28 * 2 + 1] >> 14) & 1;
            prc_middata->frmLink.cfg_mv_abs = (mb_info[28 * 2 + 1] >> 15) & 1;
            prc_middata->frmLink.cfg_ssey_thr = (mb_info[29 * 2 + 1] >> 0) & 0xFFFFFF;
            prc_middata->frmLink.cfg_ssec_thr = (mb_info[30 * 2 + 1] >> 0) & 0xFFFFFF;
            prc_middata->frmLink.frm_sady = (mb_info[15 * 2 + 1] >> 0) & 0xFFFFFFFF;
            prc_middata->frmLink.frm_sadc = (mb_info[31 * 2 + 1] >> 0) & 0xFFFFFFFF;
            prc_middata->frmLink.frm_ssey0 = (mb_info[32 * 2 + 1] >> 0) & 0xFFFFFFFF;
            prc_middata->frmLink.frm_ssey1 = (mb_info[33 * 2 + 1] >> 0) & 0xFFFFFFFF;
            prc_middata->frmLink.frm_ssec0 = (mb_info[34 * 2 + 1] >> 0) & 0xFFFFFFFF;
            prc_middata->frmLink.frm_ssec1 = (mb_info[35 * 2 + 1] >> 0) & 0xFFFFFFFF;
            prc_middata->frmLink.frm_i16 = (mb_info[23 * 2 + 1] >> 0) & 0x7FFF;
            prc_middata->frmLink.frm_i4 = (mb_info[23 * 2 + 1] >> 16) & 0x7FFF;
            prc_middata->frmLink.frm_pl0 = (mb_info[22 * 2 + 1] >> 0) & 0x7FFF;
            prc_middata->frmLink.frm_pskip = (mb_info[22 * 2 + 1] >> 16) & 0x7FFF;
            prc_middata->frmLink.frm_i8 = (mb_info[24 * 2 + 1] >> 0) & 0x7FFF;
            prc_middata->frmLink.frm_pt8 = (mb_info[24 * 2 + 1] >> 16) & 0x7FFF;
            prc_middata->frmLink.frm_mvx = (mb_info[20 * 2 + 1] >> 0) & 0x7FFFFFF;
            prc_middata->frmLink.frm_mvy = (mb_info[21 * 2 + 1] >> 0) & 0x7FFFFFF;
            prc_middata->frmLink.frm_mvdx = (mb_info[36 * 2 + 1] >> 0) & 0xFFFFFFF;
            prc_middata->frmLink.frm_mvdy = (mb_info[37 * 2 + 1] >> 0) & 0xFFFFFFF;
            prc_middata->frmLink.frm_ssey_mbs = (mb_info[38 * 2 + 1] >> 0) & 0x7FFF;
            prc_middata->frmLink.frm_ssec_mbs = (mb_info[38 * 2 + 1] >> 16) & 0x7FFF;
#endif
        }
    }
    prc_middata->actual_bits = t->p.icodec_io.bslen * 8;  //Read from VPU hardware

    if ((t->p.i_type == I264E_TYPE_IDR)) {
        t->h->common.lastidr_bits = t->p.icodec_io.bslen * 8;
        t->h->common.lastidr_qp = t->p.i_global_qp;
    }

    return 0;
}

int i264e_ratecontrol_is_reenc(i264e_thread_t *t)
{
    int breenc = 0;

    i264e_ratecontrol_end(t);

    if ((t->h->common.param.rc.i_rc_method >= I264E_RC_CQP) && (t->h->common.param.rc.i_rc_method <= I264E_RC_SMART)) {
        if ((t->h->common.param.soc == C_T21) || (t->h->common.param.soc == C_T23)) {
            t->h->common.eprc_t21.h264SliceInfo = (H264E_SLICE_INFO_S_T21*)t->p.icodec_io.protocol.h264e.H264E_SliceInfo;
            t->h->common.eprc_t21.vpuPri = t->h->thread.p.vpupri;
            t->h->common.eprc_t21.u32FrmActBs = t->p.fdec->rc_middata.actual_bits;
            breenc = JZ_VPU_RC_FRAME_REPEATE_JUDGE_T21(&t->h->common.eprc_t21);
            if (breenc) {
                i264e_log(t->h, C_LOG_DEBUG, "%s: width=%d, frameType=%d, breenc=%d\n", __func__, t->h->common.param.i_width, t->p.i_type, breenc);
            }
            t->p.b_change_ref = 0;
            if (breenc > 0) {
                t->p.i_global_qp = t->h->common.eprc_t21.u8FrmQp;
                i264e_rdposidx_t *rdpc = &t->h->common.skip_h.rdposidx_cur;
                i264e_skip_info_t *skip_info = &t->h->common.skip_h.skip_info;
                if (((skip_info->skip_type == C_STYPE_H1M_FALSE) && (rdpc->fsktype == C_FS_SBASE))
                    || ((skip_info->skip_type == C_STYPE_H1M_TRUE) && (rdpc->fsktype == C_FS_LBASE)))
                    t->p.b_change_ref = 0;
            } else {
                t->h->common.eprc_t21.u32FrmMad	  = t->p.fdec->rc_middata.pred_mad;
                t->h->common.eprc_t21.u32FrmActBs = t->p.fdec->rc_middata.actual_bits;
                t->h->common.eprc_t21.u32MvSum    = t->p.fdec->rc_middata.mb_inter_mv;
#if 0
                if (0xac == t->h->common.eprc_t21.frmLink.magic_flag) {
                    t->h->common.eprc_t21.frmLink.cfg_blk_wid = t->p.fdec->rc_middata.frmLink.cfg_blk_wid;
                    t->h->common.eprc_t21.frmLink.cfg_blk_hei = t->p.fdec->rc_middata.frmLink.cfg_blk_hei;
                    t->h->common.eprc_t21.frmLink.cfg_blk_i_thr = t->p.fdec->rc_middata.frmLink.cfg_blk_i_thr;
                    t->h->common.eprc_t21.frmLink.cfg_blk_mvr_thr1 = t->p.fdec->rc_middata.frmLink.cfg_blk_mvr_thr1;
                    t->h->common.eprc_t21.frmLink.cfg_blk_mvr_thr2 = t->p.fdec->rc_middata.frmLink.cfg_blk_mvr_thr2;
                    t->h->common.eprc_t21.frmLink.cfg_blk_mvr_thr3 = t->p.fdec->rc_middata.frmLink.cfg_blk_mvr_thr3;
                    t->h->common.eprc_t21.frmLink.blk_mvr0 = t->p.fdec->rc_middata.frmLink.blk_mvr0;
                    t->h->common.eprc_t21.frmLink.blk_mvr1 = t->p.fdec->rc_middata.frmLink.blk_mvr1;
                    t->h->common.eprc_t21.frmLink.blk_mvr2 = t->p.fdec->rc_middata.frmLink.blk_mvr2;
                    t->h->common.eprc_t21.frmLink.blk_mvr3 = t->p.fdec->rc_middata.frmLink.blk_mvr3;
                    t->h->common.eprc_t21.frmLink.blk_mvr4 = t->p.fdec->rc_middata.frmLink.blk_mvr4;
                    t->h->common.eprc_t21.frmLink.blk_mvr5 = t->p.fdec->rc_middata.frmLink.blk_mvr5;
                    t->h->common.eprc_t21.frmLink.blk_mvr6 = t->p.fdec->rc_middata.frmLink.blk_mvr6;
                    t->h->common.eprc_t21.frmLink.blk_mvr7 = t->p.fdec->rc_middata.frmLink.blk_mvr7;
                    t->h->common.eprc_t21.frmLink.blk_iw0 = t->p.fdec->rc_middata.frmLink.blk_iw0;
                    t->h->common.eprc_t21.frmLink.blk_iw1 = t->p.fdec->rc_middata.frmLink.blk_iw1;
                    t->h->common.eprc_t21.frmLink.blk_iw2 = t->p.fdec->rc_middata.frmLink.blk_iw2;
                    t->h->common.eprc_t21.frmLink.blk_iw3 = t->p.fdec->rc_middata.frmLink.blk_iw3;
                    t->h->common.eprc_t21.frmLink.cfg_info_en = t->p.fdec->rc_middata.frmLink.cfg_info_en;
                    t->h->common.eprc_t21.frmLink.cfg_mvd_all = t->p.fdec->rc_middata.frmLink.cfg_mvd_all;
                    t->h->common.eprc_t21.frmLink.cfg_mvd_abs = t->p.fdec->rc_middata.frmLink.cfg_mvd_abs;
                    t->h->common.eprc_t21.frmLink.cfg_mv_all = t->p.fdec->rc_middata.frmLink.cfg_mv_all;
                    t->h->common.eprc_t21.frmLink.cfg_mv_abs = t->p.fdec->rc_middata.frmLink.cfg_mv_abs;
                    t->h->common.eprc_t21.frmLink.cfg_ssey_thr = t->p.fdec->rc_middata.frmLink.cfg_ssey_thr;
                    t->h->common.eprc_t21.frmLink.cfg_ssec_thr = t->p.fdec->rc_middata.frmLink.cfg_ssec_thr;
                    t->h->common.eprc_t21.frmLink.frm_sady = t->p.fdec->rc_middata.frmLink.frm_sady;
                    t->h->common.eprc_t21.frmLink.frm_sadc = t->p.fdec->rc_middata.frmLink.frm_sadc;
                    t->h->common.eprc_t21.frmLink.frm_ssey0 = t->p.fdec->rc_middata.frmLink.frm_ssey0;
                    t->h->common.eprc_t21.frmLink.frm_ssey1 = t->p.fdec->rc_middata.frmLink.frm_ssey1;
                    t->h->common.eprc_t21.frmLink.frm_ssec0 = t->p.fdec->rc_middata.frmLink.frm_ssec0;
                    t->h->common.eprc_t21.frmLink.frm_ssec1 = t->p.fdec->rc_middata.frmLink.frm_ssec1;
                    t->h->common.eprc_t21.frmLink.frm_i16 = t->p.fdec->rc_middata.frmLink.frm_i16;
                    t->h->common.eprc_t21.frmLink.frm_i4 = t->p.fdec->rc_middata.frmLink.frm_i4;
                    t->h->common.eprc_t21.frmLink.frm_pl0 = t->p.fdec->rc_middata.frmLink.frm_pl0;
                    t->h->common.eprc_t21.frmLink.frm_pskip = t->p.fdec->rc_middata.frmLink.frm_pskip;
                    t->h->common.eprc_t21.frmLink.frm_i8 = t->p.fdec->rc_middata.frmLink.frm_i8;
                    t->h->common.eprc_t21.frmLink.frm_pt8 = t->p.fdec->rc_middata.frmLink.frm_pt8;
                    t->h->common.eprc_t21.frmLink.frm_mvx = t->p.fdec->rc_middata.frmLink.frm_mvx;
                    t->h->common.eprc_t21.frmLink.frm_mvy = t->p.fdec->rc_middata.frmLink.frm_mvy;
                    t->h->common.eprc_t21.frmLink.frm_mvdx = t->p.fdec->rc_middata.frmLink.frm_mvdx;
                    t->h->common.eprc_t21.frmLink.frm_mvdy = t->p.fdec->rc_middata.frmLink.frm_mvdy;
                    t->h->common.eprc_t21.frmLink.frm_ssey_mbs = t->p.fdec->rc_middata.frmLink.frm_ssey_mbs;
                    t->h->common.eprc_t21.frmLink.frm_ssec_mbs = t->p.fdec->rc_middata.frmLink.frm_ssec_mbs;
                }
#endif
                t->h->common.eprc_t21.frmLink.magic_flag |= 2;

                memcpy(t->h->common.eprc_t21.u32FrmMvrRes, t->p.fdec->rc_middata.mv_range,     sizeof(t->h->common.eprc_t21.u32FrmMvrRes));
                memcpy(t->h->common.eprc_t21.u16FrmIwRes,  t->p.fdec->rc_middata.intra_weight, sizeof(t->h->common.eprc_t21.u16FrmIwRes));
                t->h->common.eprc_t21.u32MvCnt  = t->p.fdec->rc_middata.glb_mv_cnt;
                t->h->common.eprc_t21.u32MvxSum = t->p.fdec->rc_middata.glb_mvx_sum;
                t->h->common.eprc_t21.u32MvySum = t->p.fdec->rc_middata.glb_mvy_sum;
                //t->h->common.eprc_t21.u32MbInterCnt = t->p.fdec->rc_middata.mb_inter_cnt;
                t->h->common.eprc_t21.u32FrmI16Cnt = (t->p.fdec->rc_middata.frm_i4Cnt / 16)+(t->p.fdec->rc_middata.frm_i8Cnt/4)+t->p.fdec->rc_middata.frm_i16Cnt;
                memcpy(t->h->common.eprc_t21.rgnc,t->p.fdec->rc_middata.qpg_rgnc,sizeof(t->h->common.eprc_t21.rgnc));
                JZ_VPU_RC_FRAME_END_T21(&t->h->common.eprc_t21);
            }
        }
    }

    return breenc;
}

/* used to judge whether to use the old or new bs by set thread private member b_change_ref to 0 or 1 */
int i264e_ratecontrol_is_reenc_end(i264e_thread_t *t)
{
    i264e_rdposidx_t *rdpc = &t->h->common.skip_h.rdposidx_cur;
    i264e_skip_info_t *skip_info = &t->h->common.skip_h.skip_info;

    if (((skip_info->skip_type == C_STYPE_H1M_FALSE) && (rdpc->fsktype == C_FS_SBASE))
        || ((skip_info->skip_type == C_STYPE_H1M_TRUE) && (rdpc->fsktype == C_FS_LBASE))) {
            if (t->p.reenc_tmp_info.i_old_bslen > t->p.reenc_tmp_info.i_cur_bslen)
            t->p.b_change_ref = 1;
            else
            t->p.b_change_ref = 0;

        }

    return 0;
}

int i264e_ratecontrol_is_larger_lastidr(i264e_thread_t *t)
{
    if ((t->h->common.lastidr_bits) < t->p.icodec_io.bslen * 8) {
        return 1;
    }
    return 0;
}

void i264e_ratecontrol_deinit(i264e_t *h)
{
    if ((h->common.param.rc.i_rc_method >= I264E_RC_CBR) || (h->common.param.rc.i_rc_method <= I264E_RC_SMART)) {
        if ((h->common.param.soc == C_T21) || (h->common.param.soc == C_T23)) {
            JZ_VPU_RC_FREE_T21(&h->common.eprc_t21);
        }
    }
}
